package com.example.animationmodule

import android.graphics.drawable.AnimationDrawable
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.util.Log
import android.view.View
import android.view.animation.*
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import java.util.*

class TweenActivity : AppCompatActivity() {
    private  val TAG = "TweenActivity"
    private lateinit var imageView: ImageView
    val timer= Timer()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_tween)
        this.imageView=findViewById(R.id.iv_animation)
    }

    private val handler=object:Handler(Looper.getMainLooper()){
        override fun handleMessage(msg: Message) {
            when(msg.what){
                1 -> {
                    Log.d(TAG, "handleMessage: x=${imageView.x} y=${imageView.y}")
                }
            }
        }
    }
    fun startTranslation(view: View) {
        Log.d(TAG, "startTranslation: ")
        val translateAnimation = TranslateAnimation(
                Animation.RELATIVE_TO_PARENT, 0.toFloat(),
                Animation.RELATIVE_TO_PARENT, 0.5.toFloat(),
                Animation.RELATIVE_TO_PARENT, 0.toFloat(),
                Animation.RELATIVE_TO_PARENT, 0.5.toFloat()
        )


        //设置持续时间
        translateAnimation.duration=2000
        //设置重复次数
//        translateAnimation.repeatCount=Animation.INFINITE
        //设置最后是否填充
        translateAnimation.fillAfter=false

//        translateAnimation.repeatMode=Animation.RESTART
        this.imageView.startAnimation(translateAnimation)

        timer.scheduleAtFixedRate(object:TimerTask(){
            override fun run() {
//                TODO("Not yet implemented")
                handler.sendEmptyMessage(1)
            }

        },5,100)

    }

    fun startScale(view: View) {
            this.timer.cancel()
        val scaleAnimation = ScaleAnimation(0f, 0.5f, 0f, 1f)
        scaleAnimation.duration=2000
        this.imageView.startAnimation(scaleAnimation)
    }
    fun startRotate(view: View) {
        val rotateAnimation = RotateAnimation(0f, 360f)
        rotateAnimation.duration=2000
        this.imageView.startAnimation(rotateAnimation)
    }
    fun startAlpha(view: View) {
        val alphaAnimation = AlphaAnimation(0.toFloat(), 1.toFloat())
        alphaAnimation.duration=2000
        this.imageView.startAnimation(alphaAnimation)
    }

    fun startSet(view: View) {
        val loadAnimation = AnimationUtils.loadAnimation(this, R.anim.set_anim)
        this.imageView.startAnimation(loadAnimation)
    }
}
